<template>
  <div class="main-contain home" style="width: 1400px;margin: 1px auto;overflow: hidden;">
    <!-- 横向轮播图 -->
    <el-carousel class="carousel" :interval="3000" type="card" height="480px">
      <el-carousel-item v-for="(item, index) in slidePics" :key="index">
        <img :src="item.picUrl" class="carousel-image" />
        <div class="carousel-title"></div>
      </el-carousel-item>
    </el-carousel>

    <!-- <el-carousel indicator-position="outside" :autoplay="true" height="480px" class="carousel-contain">
      <el-carousel-item class="carousel-item" v-for="(item, key) in slidePics" :key="key">
        <a :href="item.link" target="_blank" v-if="item.clickUrl!=''">
          <img :src="item.picUrl" alt=""/>
        </a>
        <img :src="item.picUrl" alt="" v-else/>
      </el-carousel-item>
    </el-carousel> -->

    <!-- 热门资讯内容列表 -->
    <!--列表-->
    <div class="index-row">
      <div class="title" style="color:#d77f57;">热门内容</div>
      <div class="row-container" v-if="productList">
        <el-card class="article-list-card" v-for="article in productList" :key="article.courseId"
          @click="toNewsDetailPage(article.id)">
          <div class="article-list article-item">
            <!-- 缩略图 -->
            <!-- 标题和发布时间 -->
            <div class="article-info">
              <el-image :src="article.mainPic" class="article-thumbnail" fit="cover"></el-image>
              <div class="article-title">
                {{ article.title }}
              </div>
              <div class="atl-tag">
                <el-tag type="primary">{{ article.tags }}</el-tag>
              </div>
              <div class="article-meta">
                <span class="publish-date">发布时间： {{ timestampToDayHM(article.updatedAt) }}</span>
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick, toRef } from "vue";
import baseService from "@/service/baseService";
import { ElMessage, ElMessageBox } from "element-plus";
import { timestampToDayHM, timestampToHM } from "@/utils/utils"
import { fileUploader } from '@/utils/upload.js'
import { useRouter, useRoute } from 'vue-router';

// 查询参数
const queryForm = reactive({
  deptName: "",
  pageNo: 1,
  pageSize: 10
});
const router = useRouter();
const route = useRoute();
// 列表内容数量
const total = ref(0);
// 列表是否加载
const loading = ref(true);
// 列表返回值
const list = ref<any[]>([]);

// 表单实例
const formRef = ref();
// 是否打开弹出框
const open = ref(false);
const openDetail = ref(false);
// 弹出框标题
const title = ref("");
const productList = ref<any[]>([]);
const slidePics = ref();
const shopId = ref();

// 表单验证
const rules = ref({
  deptName: [{ required: true, message: "规则名称不能为空", trigger: "blur" }],
});

function goBack() {
  router.back()
}

const getInfo = () => {
  // const infoId = route.query.pid
  // console.log('从路由获取参数：', route.query)
  // shopId.value = infoId
  // loading.value = true;
  // baseService.get(`/admin/product/info/${infoId}`).then((res) => {
  //   console.log('获取商品信息：',res)
  //   product.value = res.data
  //   loading.value = false;
  // }).catch(() => {
  //   loading.value = false;
  // });
  loading.value = true;
  baseService.get(`/api/index/slide/pictures`).then((res) => {
    console.log('获取轮播图：', res)
    slidePics.value = res.data
    loading.value = false;
  }).catch(() => {
    loading.value = false;
  });

  baseService.get(`/api/index/rec/products`).then((res) => {
    console.log('获取热门内容：', res)
    productList.value = res.data.news
    loading.value = false;
  }).catch(() => {
    loading.value = false;
  });
}

const toCourseDetailPage = (pid: number) => {
  router.push("/course/detail?nid=" + pid);
}

const toNewsDetailPage = (pid: number) => {
  router.push("/display/detail?nid=" + pid);
}

getInfo();
</script>

<style scoped>
.carousel-item img {
  width: 100%;
  height: auto;
}

.carousel-contain {
  width: 1020px;
}

.index-row {
  margin-bottom: 10px;
  margin-top: 15px;
  padding-bottom: 10px;
  border-bottom: #ccc 1px solid;
  clear: both;
  width: 1020px;
  margin: 15px auto;
}

.index-row .title {
  font-size: 22px;
  text-align: center;
  line-height: 50px;
  border-bottom: 2px solid #ccc;
  margin-bottom: 20px;
  clear: both;
}

.item-list li:HOVER .item-name {
  color: #1ea5a3;
  cursor: pointer;
}

.item-list li:HOVER {
  cursor: pointer;
  border: #1ea5a3 solid 1px;
}

.item-list {
  display: flex;
  width: 82%;
  flex-wrap: wrap;
}

.item-list li {
  display: inline-block;
  width: 20%;
  padding: 15px;
  box-sizing: border-box;
  text-align: center;
  cursor: pointer;
  border: #fff solid 1px;
}

.item-list li p {
  margin: 5px 0;
  font-size: 14px;
}

.item-list .item-img img {
  width: 120px;
  height: 100px;
}

.item-list .item-price {
  color: #d77f57;
}

.item-list .item-name {
  color: #666;
  padding-left: 6px;
  font-size: 12px;
}

.row-container {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}

.article-list-card {
  color: #555;
  margin-bottom: 30px;
  width: 48%;
  box-sizing: border-box;
}

.list-title {
  font-size: 24px;
  font-weight: normal;
  margin: 0 0 20px 0;
  text-align: center;
  color: #c35556;
}

.article-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.article-item {
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.article-item:hover {
  background-color: #f5f5f5;
}

.article-item:hover .article-title {
  color: #2a5caa;
}

.article-thumbnail {
  width: 140px;
  height: 100px;
  border-radius: 4px;
  float: left;
  margin-right: 10px;
}

.article-info {
  float: left;
}

.article-title {
  font-size: 18px;
  margin: 0 0 10px 0;
}

.article-meta {
  font-size: 14px;
  color: #666;
}

.publish-date {
  font-style: italic;
}

.atl-tag,
.atl-like {
  margin: 5px 0;
}

.atl-like {
  font-size: 13px;
}

.atl-like span {
  padding: 0 10px;
}

.home {
  display: flex;
  flex-direction: column;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  background-color: #409EFF;
  color: white;
}

.logo {
  font-size: 24px;
  font-weight: bold;
}

.carousel {
  margin: 20px 0;
}

.carousel-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>